package com.caigou.modules.hybrid.ui

import android.graphics.Color
import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseActivity
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.hybrid.databinding.ActivityCalendarBinding
import com.tencent.smtt.export.external.interfaces.SslError
import com.tencent.smtt.export.external.interfaces.SslErrorHandler
import com.tencent.smtt.sdk.*
import dagger.hilt.android.AndroidEntryPoint
import java.util.*

/**
 * @author #Suyghur.
 * Created on 2021/12/29
 */
@Route(path = RouterPath.Hybrid.PAGE_CALENDAR)
@AndroidEntryPoint
class CalendarActivity : BaseActivity() {
    private lateinit var binding: ActivityCalendarBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityCalendarBinding.inflate(layoutInflater)
        setContentView(binding.root)
        containerId = binding.calendarFlContainer.id
        initView()
        mDialog.show()
    }

    private fun initView() {
        ARouter.getInstance().inject(this)
        binding.calendarWebviewContainer.run {

            isVerticalScrollBarEnabled = true
            scrollBarStyle = View.SCROLLBARS_INSIDE_OVERLAY
            setBackgroundColor(Color.TRANSPARENT)

            val webSetting = settings
            webSetting.allowFileAccess = true
            webSetting.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
            webSetting.setSupportZoom(true)
            webSetting.saveFormData = true
            webSetting.savePassword = true
            webSetting.builtInZoomControls = false
            webSetting.useWideViewPort = true
            webSetting.setSupportMultipleWindows(false)
            webSetting.setAppCacheEnabled(false)
            webSetting.domStorageEnabled = true
            webSetting.javaScriptEnabled = true
            webSetting.setGeolocationEnabled(true)
            webSetting.setAppCacheMaxSize(Long.MAX_VALUE)
            webSetting.setAppCachePath(this@CalendarActivity.getDir("appcache", 0).path)
            webSetting.databasePath = this@CalendarActivity.getDir("databases", 0).path
            webSetting.setGeolocationDatabasePath(this@CalendarActivity.getDir("geolocation", 0).path)
            webSetting.pluginState = WebSettings.PluginState.ON_DEMAND
            webSetting.cacheMode = WebSettings.LOAD_NO_CACHE
            CookieSyncManager.createInstance(this@CalendarActivity)
            CookieSyncManager.getInstance().sync()

            loadUrl("https://8888188tt.com/index.html#/home/LaoHuangLi")

            webViewClient = object : WebViewClient() {
                override fun onPageFinished(view: WebView, url: String) {
                    super.onPageFinished(view, url)
                    mDialog.dismiss()
                }

                override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                    Logger.d("shouldOverrideUrlLoading, url: $url")
                    val hitTestResult = view.hitTestResult
                    if (url.startsWith("http:") || url.startsWith("https:")) {
                        //解决重定向加载的问题 return false
                        if (!view.canGoBack()) {
                            return false
                        }
                        //解决重定向加载的问题 return false
                        if (hitTestResult.type == WebView.HitTestResult.UNKNOWN_TYPE) {
                            return false
                        }
                        view.loadUrl(url)
                    }
                    return false
                }

                override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) {
                    super.onReceivedSslError(view, handler, error)
                }
            }

            webChromeClient = object : WebChromeClient() {
                override fun onProgressChanged(view: WebView, newProgress: Int) {
                    super.onProgressChanged(view, newProgress)
                }

                override fun onReceivedTitle(view: WebView, title: String) {
                    super.onReceivedTitle(view, title)
                }
            }
        }

        binding.calendarTvBack.setOnClickListener {
            finish()
        }
    }
}